package com.wl.aop;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;

public class ClassDemo {
    public static void main(String[] args)
            throws NoSuchMethodException,
            InvocationTargetException,
            IllegalAccessException {
        A a = new A();
        A b = new B();

        //不通过反射调用 a 方法，看报错
        //a.callTest("张三");

        showObject(a);
        System.out.println("-------");
        showObject(b);
    }

    public static void showObject(Object object) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Class<?> aClass = object.getClass();
        System.out.println("aClass.getName() = "+aClass.getName());
        System.out.println("aClass.getSimpleName() = "+aClass.getSimpleName());

        for (Field field : aClass.getFields()) {
            System.out.println("field =" + field);
        }


        for (Field declaredField : aClass.getDeclaredFields()) {
            System.out.println("declaredField=" + declaredField);
        }

        Method test = aClass.getDeclaredMethod("test", String.class);
        if(test != null){
            //设置强制访问
            test.setAccessible(true);
            test.invoke(object,"张三");
        }
    }
}
class A{
    private int a;
    private String b;
    private Byte c;
    private Date d;

    protected void test(int num){

    }

    public void callTest(String s){
        test(s);
    }

    private void test(String s){
        int i = 1/0;
        System.out.println("hello " + s);
    }
}

class B extends A{

}

